using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerManager:MonoBehaviour
{
    private static PlayerManager instance ;
    
    

    public static PlayerManager getInstance()
    {
        return instance;
    }

    public Player mainPlayer;//主玩家
    public Dictionary<string, Player> ohterPlayers = new Dictionary<string, Player>();//其他玩家


    private void Awake()
    {
        instance = this;
    }

    private void Update()
    {
        playersMove();
    }

    public void playersMove()
    {
        playerMove(mainPlayer);
        foreach (var ohterPlayer in ohterPlayers)
        {
            playerMove(ohterPlayer.Value);
        }
    }
    public void playerMove(Player player)
    {
        if (player == null)
        {
            return;
        }

        List<Vector3> playerPoints = player.points;
        if (playerPoints.Count > 0)
        {
            Vector3 target = playerPoints[0];
            if (Vector3.Distance(player.obj.transform.position, target) < 0.1f)
            {
                //去下一个点
                playerPoints.RemoveAt(0);
                if (playerPoints.Count > 0)
                {
                    target = playerPoints[0];
                    player.obj.transform.position = Vector3.MoveTowards(player.obj.transform.position, target, player.speed * Time.deltaTime);
                }

            }
            else
            {
                player.obj.transform.position = Vector3.MoveTowards(player.obj.transform.position, target, player.speed * Time.deltaTime);
            }
        }
    }
}
